// todos相关axios
import axios from 'axios'
// 获取任务列表
export const getList = () => {
  return async (dispatch) => {
    const res = await axios.get('http://localhost:8888/todos')
    dispatch({
      type: 'todo/getList',
      list: res.data,
    })
  }
}

// 添加任务
export const add = (name) => {
  return async (dispatch) => {
    await axios.post('http://localhost:8888/todos', {
      name,
      isDone: false,
    })
    dispatch(getList())
  }
}

// 删除任务
export const del = (id) => {
  return async (dispatch) => {
    await axios.delete('http://localhost:8888/todos/' + id)
    // dispatch(getList())
    dispatch({
      type: 'todo/del',
      payload: id,
    })
  }
}

// 修改任务状态
export const updateState = (id, data) => {
  return async (dispatch) => {
    await axios.put('http://localhost:8888/todos/' + id, data)
    // await axios.patch('http://localhost:8888/todos/' + id, {
    //   isDone: data.isDone,
    // })
    dispatch(getList())
    // dispatch({
    //   type: 'todo/updataState',
    //   state_id: id,
    // })
  }
}



// 全选
export const checkedAll = (checked, list) => {
  return (dispatch) => {
    list.forEach(async (item) => {
      await axios.put('http://localhost:8888/todos/' + item.id, {
        id: item.id,
        name: item.name,
        isDone: checked,
      })
    })
    dispatch(getList())
  }
}
